package com.sojpt.boot.controller.business;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.controller.business.validator.LkyTeacherAddValidator;
import com.sojpt.boot.controller.business.validator.LkyTeacherUpdateValidator;
import com.sojpt.boot.intercepter.admin.AdminLoginInterceptor;
import com.sojpt.boot.intercepter.admin.AdminPopedomInterceptor;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.SoJLogKit;
import com.sojpt.model.DicPopedom;
import com.sojpt.model.LkyClassTeacher;
import com.sojpt.model.LkyTeacher;
import com.sojpt.util.ResultUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 *
 * @author Sohnny
 *
 */
@RestController
@RequestMapping("/admin/teacherClass/")
//@Before({AdminLoginInterceptor.class, AdminPopedomInterceptor.class})
public class LkyTeacherClassAdminController extends IViewController {
    @GetMapping("list")
    public void list() {
        LkyTeacher lkyTeacher = getModel(LkyTeacher.class, "lkyTeacher");
        Map<String,String> map =new HashMap<>();
        map.put("cid",getPara("cid"));
        map.put("mid",getPara("mid"));
        map.put("coid",getPara("coid"));
        map.put("name",lkyTeacher.getName());
        SqlPara sqlPara = Db.getSqlPara("lky_teacher_class.select", map);
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    @Before(LkyTeacherAddValidator.class)
    @PostMapping("save")
    public void save() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/teacherClass/save");
        LkyTeacher lkyTeacher = getModel(LkyTeacher.class, "lkyTeacher");
        lkyTeacher.setId(UUID.randomUUID().toString().replace("-", ""));
        lkyTeacher.setCreationTime(new Date());
        lkyTeacher.setUpdateTime(new Date());
        if(lkyTeacher.save()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("添加成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("删除成功");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @Before(LkyTeacherUpdateValidator.class)
    @PostMapping("update")
    public void update() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/teacherClass/update");
        LkyTeacher lkyTeacher = getModel(LkyTeacher.class, "lkyTeacher");
        lkyTeacher.setUpdateTime(new Date());
        if(lkyTeacher.update()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("更新成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("更新成功");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("delete")
    public void delete() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/teacherClass/delete");
        LkyTeacher lkyTeacher = getModel(LkyTeacher.class, "lkyTeacher");
        if(LkyTeacher.dao.deleteById(lkyTeacher.getId())) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("删除失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("delByTCId")
    public void deleteByTCId() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/teacherClass/delByTCId");
        LkyClassTeacher lkyClassTeacher = getModel(LkyClassTeacher.class, "lkyClassTeacher");
        if(LkyClassTeacher.dao.deleteById(lkyClassTeacher.getId())) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("删除失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("batchDelete")
    public void batchDelete() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/teacherClass/batchDelete");
        String[] ids = getPara("ids").split(",");
        SqlPara sqlPara = Db.getSqlPara("lky_teacher_class.deleteByIds", Kv.by("idList", Arrays.asList(ids)));
        if(Db.update(sqlPara) > 0) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("批量删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("批量删除失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    //老师管理-分配班级的老师已分配班级查询
    @PostMapping("selectClassByTid")
    public void selectClassByTid() {
        String tid = getPara("tid");
        SqlPara sqlPara = Db.getSqlPara("lky_teacher_class.selectClassByTid", Kv.by("tid", tid));
        List<Record> records = Db.find(sqlPara);
        renderJson(ResultUtil.success(records));
//        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
//        renderJson(page);
    }
}